<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017-10-05
 * Time: 23:17
 */

namespace app\Http\Controllers\Wechat;


use App\Helpers\QiniuHelper;
use App\Http\Controllers\Controller;
use App\Models\WechatUpload;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;

class QiniuController extends Controller
{
    public function getUploadToken(Request $request)
    {
        $arr['uptoken'] = '';
        $policy = [];
        switch ((int) $request->type){
            case 1 :
                $arr['key'] = QiniuHelper::imageKey();
                $returnBody = '{"key":"$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)","name":"$(x:name)","persistentId":"$(persistentId)"}';
                $blur = 'imageMogr2/blur/20x15|saveas/' . \Qiniu\base64_urlSafeEncode(env('QINIU_BUCKET') . ":{$arr['key']}_blur.jpg");
                $show = 'imageView2/0/h/1024/format/jpg|saveas/' . \Qiniu\base64_urlSafeEncode(env('QINIU_BUCKET') . ":{$arr['key']}.jpg");
                $policy = [
                    'returnBody' => $returnBody,
                    'persistentOps' => $blur . ";" . $show,
                ];
                break;
            case 2:
                $arr['key'] = QiniuHelper::videoKey();
                $returnBody = '{"key":"$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)","name":"$(x:name)","persistentId":"$(persistentId)","duration":"$(avinfo.video.duration)"}';
                $convertEntry = \Qiniu\base64_urlSafeEncode(env('QINIU_BUCKET') . ":{$arr['key']}.mp4");
                $convertFop = 'avthumb/mp4/vcodec/libx264/acodec/libfaac/stripmeta/1|saveas/' . $convertEntry;
                $thumbEntry = \Qiniu\base64_urlSafeEncode(env('QINIU_BUCKET') . ":{$arr['key']}_thumb.jpg");
                $thumbFop = 'vframe/jpg/offset/3|saveas/' . $thumbEntry;
                $policy = array(
                    'returnBody' => $returnBody,
                    'persistentOps' => $convertFop . ";" . $thumbFop,
                    'persistentPipeline' => env('QINIU_PIPELINE')
                );
                break;
        }
        $arr['uptoken'] = QiniuHelper::getUploadToken($arr['key'], 3600, $policy);
        return response()->json($arr);
    }

    public function postUploadToken(Request $request)
    {
        $wechatUpload = new WechatUpload();
        $wechatUpload->key = $request->key;
        $wechatUpload->openid = Session::get('wechat_openid');
        $wechatUpload->type = $request->type;
        if (null !== $request->duration) {
            $wechatUpload->duration = $request->duration;
        }
        $wechatUpload->status = 2;
        $wechatUpload->save();
        // 保存用户的视频
        $arr['code'] = 1;
        $arr['url'] = '/wechat/set-price?id=' . $wechatUpload->id;
        return response()->json($arr);
    }

    public function postPrefop(Request $request)
    {
        $persistentId = $request->persistentId;
        $result = json_decode(json_encode(QiniuHelper::getPersistentFop()->status($persistentId)), true);
        if (!isset($result[0]['items'])) {
            return '';
        }
        foreach ($result[0]['items'] as $item) {
            if (!isset($item['code'])) {
                return '';
            }
            switch ($item['code']) {
                case 0:
                    break;
                case 1:
                case 2:
                    return '';
                    break;
                case 3:
                    return '0';
            }
        }
        echo 1;
        exit;
    }
}